{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![LOGO](../img/MODIN_ver2_hrz.png)\n",
    "\n",
    "<center><h2>Scale your pandas workflows by changing one line of code</h2>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 4: Setting up cluster environment\n",
    "\n",
    "**GOAL**: Learn how to set up a cluster for Modin.\n",
    "\n",
    "**NOTE**: This exercise has extra requirements. Read instructions carefully before attempting. \n",
    "\n",
    "**This exercise instructs the user on how to start a 700+ core cluster, and it is not shut down until the end of Exercise 5. Read instructions carefully.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Often in practice we have a need to exceed the capabilities of a single machine. Modin works and performs well in both local mode and in a cluster environment. The key advantage of Modin is that your notebook does not change between local development and cluster execution. Users are not required to think about how many workers exist or how to distribute and partition their data; Modin handles all of this seamlessly and transparently.\n",
    "\n",
    "![Cluster](../img/modin_cluster.png)\n",
    "\n",
    "**Extra Requirements for this exercise**\n",
    "\n",
    "Detailed instructions can be found here: https://docs.ray.io/en/master/cluster/launcher.html\n",
    "\n",
    "From command line:\n",
    "- `pip install boto3`\n",
    "- `aws configure`\n",
    "- `ray up modin-cluster.yaml`\n",
    "\n",
    "Included in this directory is a file named `modin-cluster.yaml`. We will use this to start the cluster."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install boto3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !aws configure"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Starting and connecting to the cluster\n",
    "\n",
    "This example starts 1 head node (m5.24xlarge) and 7 workers (m5.24xlarge), 768 total CPUs.\n",
    "\n",
    "Cost of this cluster can be found here: https://aws.amazon.com/ec2/pricing/on-demand/."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !ray up modin-cluster.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Connect to the cluster with `ray attach`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !ray attach modin-cluster.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# DO NOT CHANGE THIS CODE!\n",
    "# Changing this code risks breaking further exercises\n",
    "\n",
    "import time\n",
    "time.sleep(600) # We need to give ray enough time to start up all the workers\n",
    "import ray\n",
    "ray.init(address=\"auto\")\n",
    "from modin.config import NPartitions\n",
    "assert NPartitions.get() == 768, \"Not all Ray nodes are started up yet\"\n",
    "ray.shutdown()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Please move on to Exercise 5"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
